package org.dolphin.dao.impl;

import java.io.Serializable;
import java.util.List;
import org.dolphin.base.bean.ProductCategoryVO;
import org.dolphin.dao.ProductCategoryDao;
import org.dolphin.dao.util.SessionHelper;
import org.dolphin.entity.inventory.ProductCategory;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository(value = "productCategoryDao")
public class ProductCategoryDaoImpl implements ProductCategoryDao {
  @Autowired(required = true)
  private SessionFactory sessionFactory;

  protected DetachedCriteria buildProductCategoryCriteria() {
    DetachedCriteria dc = DetachedCriteria.forClass(ProductCategory.class);
    return dc;
  }

  @Override
  public ProductCategory fetchProductCategory(Serializable id) {
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    ProductCategory productCategory = (ProductCategory) session.get(ProductCategory.class, id);
    return productCategory;
  }

  @SuppressWarnings("unchecked")
  @Override
  public List<ProductCategoryVO> getProductCategories(int firstResult, int maxResults,
      List<Order> orders) {
    DetachedCriteria dc = buildProductCategoryCriteria();
    if (orders != null && !orders.isEmpty()) {
      for (Order order : orders) {
        dc.addOrder(order);
      }
    }
    ProjectionList projectionList = Projections.projectionList();
    projectionList.add(Projections.id(), "id");
    projectionList.add(Projections.property("name"), "name");
    projectionList.add(Projections.property("description"), "description");
    projectionList.add(Projections.property("version"), "version");
    dc.setProjection(projectionList);
    dc.setResultTransformer(new AliasToBeanResultTransformer(ProductCategoryVO.class));
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    Criteria criteria = dc.getExecutableCriteria(session);
    if (firstResult > 0) {
      criteria.setFirstResult(firstResult);
    }
    if (maxResults > 0) {
      criteria.setMaxResults(maxResults);
    }
    List<ProductCategoryVO> list = criteria.list();
    return list;
  }

  @Override
  public Long getProductCategoriesCount() {
    DetachedCriteria dc = buildProductCategoryCriteria();
    dc.setProjection(Projections.rowCount());
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    Criteria criteria = dc.getExecutableCriteria(session);
    Number totalRecords = (Number) criteria.uniqueResult();
    return totalRecords.longValue();
  }

  @Override
  public ProductCategory getProductCategory(String name) {
    ProductCategory category = getProductCategory(name, null);
    return category;
  }

  @SuppressWarnings("unchecked")
  @Override
  public ProductCategory getProductCategory(String name, Serializable excludedId) {
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    DetachedCriteria dc = buildProductCategoryCriteria();
    dc.add(Restrictions.eq("name", name));
    if (excludedId != null) {
      dc.add(Restrictions.not(Restrictions.idEq(excludedId)));
    }
    Criteria criteria = dc.getExecutableCriteria(session);
    List<ProductCategory> list = criteria.list();
    if (list != null && !list.isEmpty()) {
      return list.get(0);
    }
    return null;
  }

  @SuppressWarnings("unchecked")
  @Override
  public List<ProductCategory> listProductCategory() {
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    Criteria criteria = session.createCriteria(ProductCategory.class);
    criteria.addOrder(Order.asc("name"));
    List<ProductCategory> list = criteria.list();
    return list;
  }

  @Override
  public ProductCategory loadProductCategory(Serializable id) {
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    ProductCategory productCategory = (ProductCategory) session.load(ProductCategory.class, id);
    return productCategory;
  }

  @Override
  public void saveProductCategory(ProductCategory category) {
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    session.save(category);
  }

  @Override
  public void updateProductCategory(ProductCategory category) {
    Session session = SessionHelper.getCurrentSession(sessionFactory);
    session.update(category);
  }
}
